package tk.mybatis.mapper.entity;

import java.util.Set;

import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import tk.mybatis.mapper.mapperhelper.EntityHelper;
import tk.mybatis.mapper.mapperhelper.SqlHelper;
import tk.mybatis.mapper.mapperhelper.SqlNestdHelper;

/**
 * 
 * title: NestdEntity.java 
 * description
 *
 * @author rplees
 * @email rplees.i.ly@gmail.com
 * @version 1.0  
 * @created Jun 29, 2016 2:44:42 PM
 */
public class NestdEntity {
	JoinColumn joinColumn;
	JoinColumns joinColumns;
	EntityTable entityTable;
	EntityField field;
	NestdEntity upLevelNestdEntity;
	
	public NestdEntity(EntityField field, JoinColumn joinColumn, JoinColumns joinColumns, NestdEntity upLevelNestdEntity) {
		this.field = field;
		this.joinColumn = joinColumn;
		this.joinColumns = joinColumns;
		this.upLevelNestdEntity = upLevelNestdEntity;
		this.entityTable = EntityHelper.getEntityTable(field.getRealEntityTableJavaType());
	}
	
	public EntityTable getEntityTable() {
		return entityTable;
	}
	
	public String getColumn(JoinColumn joinColumn) {
		if(joinColumn != null) {
			return joinColumn.name();
		}
		return getProperty();
	}
	
	String getPk(JoinColumn joinColumn) {
		String pk = "";
		if(joinColumn != null && !StringUtils.isEmpty(joinColumn.referencedColumnName())) {
			pk = joinColumn.referencedColumnName();
		}
		
		if(StringUtils.isEmpty(pk)) {
			Set<EntityColumn> pkColumns = entityTable.getEntityClassPKColumns();
			Assert.isTrue(pkColumns != null && pkColumns.size() == 1, "一对一的外键不能有多个");
			pk = pkColumns.iterator().next().getColumn();
		}
		
		return pk;
	}
	
	public EntityField getField() {
		return field;
	}

	public void setField(EntityField field) {
		this.field = field;
	}

	/**
	 * 返回 LEFT OUTER JOIN TablePK AS mytable ON mytableID = mytable.id
	 * @return
	 */
	public String getLeftJoinOnTmpl(String mainTableAlias, NestdEntityContext helper, Config config) {
		String alias = SqlNestdHelper.doKeyword(helper.getAliasSuffix());
		String tableName = SqlNestdHelper.doKeyword(SqlNestdHelper.tableName(field.getRealEntityTableJavaType(), config));
		
		StringBuffer buffer = new StringBuffer(SqlHelper.SPLIT_ENTER_SEPARATOR + "LEFT OUTER JOIN ").append(tableName).append(" AS ").append(alias).append(" ON ");
		
		if(joinColumns != null) {//复合条件
			JoinColumn[] columns = joinColumns.value();
			if(columns == null || columns.length < 1) {
				buffer.append(mainTableAlias + ".").append(getColumn(null))
				.append("=").append(alias).append(".").append(getPk(null));
			} else {
				for (int i = 0; i < columns.length; i++) {
					buffer.append(mainTableAlias + ".").append(getColumn(columns[i]))
					.append("=").append(alias).append(".").append(getPk(columns[i]));
					
					if(i != columns.length - 1) {
						buffer.append(" AND ");
					}
				}
			}
		} else {
			buffer.append(mainTableAlias + ".").append(getColumn(joinColumn))
			.append("=").append(alias).append(".").append(getPk(joinColumn));
		}
		
		return buffer.toString();
	}

	/**
	 * 属性
	 * 外键的字段名字
	 * @return
	 */
	public String getProperty() {
		return field.getName();
	}
	
}